from manimlib import *

b = [1, -1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66, 0, -691/2730,
     0, 7/6, 0, -3617/510, 0, 43867/798, 0, -174611/330, 0, 854513/138, 0]
e = [1, 1, 5, 61, 1385, 50521, 2702765, 199360981, 19391512145, 2404879675441, 370371188237525, 69348874393137901,
     15514534163557086905, 4087072509293123892361, 1252259641403629865468285, 441543893249023104553682821, 177519391579539289436664789665]
#############################################################


def taylor_for_sin(degree):
    def sin(t):
        s = sum([(-1)**n/math.factorial(2*n+1)*t**(2*n+1)
                for n in range(0, int(degree)+1)])
        return s
    return sin


def taylor_for_cos(degree):
    def cos(t):
        s = sum([(-1)**n/math.factorial(2*n)*t**(2*n)
                for n in range(0, int(degree)+1)])
        return s
    return cos


def taylor_for_tan(degree):
    if degree == 0:
        return lambda _: 0
    else:
        def tan(t):
            s = sum([b[2*n]*(-4)**n*(1-4**n)/math.factorial(2*n)*t**(2*n-1)
                    for n in range(1, int(degree)+1)])
            return s
        return tan


def taylor_for_cot(degree):
    def cot(t):
        s = sum([(-4)**n*b[2*n]/math.factorial(2*n)*t**(2*n-1)
                for n in range(0, int(degree)+1)])
        return s
    return cot


def taylor_for_sec(degree):
    def sec(t):
        s = sum([(t**(2*n))*e[n]/math.factorial(2*n)
                for n in range(0, int(degree)+1)])
        return s
    return sec


def taylor_for_csc(degree):
    def csc(t):
        s = sum([(-1)**(n+1)*(4**n-2)*b[2*n]/math.factorial(2*n)*t**(2*n-1)
                for n in range(0, int(degree)+1)])
        return s
    return csc
############################################################


def taylor_for_sin_partly(degree):
    def sin(n):
        return lambda t: (-1)**n/math.factorial(2*n+1)*t**(2*n+1)
    return [sin(n) for n in range(0, int(degree))]


def taylor_for_cos_partly(degree):
    def cos(n):
        return lambda t: (-1)**n/math.factorial(2*n)*t**(2*n)
    return [cos(n) for n in range(0, int(degree))]
#############################################################


def get_path(o, i):
    path = o.family_members_with_points()[i]
    path.set_fill(opacity=0)
    path.set_stroke(WHITE, 1)
    return path


def formulaparts(n, name):
    if n == 0:
        return [name+"(0)"]
    elif n == 1:
        return ["{", name+"^{(1)}(0)", "\\over", "1!", "}", "x"]
    else:
        return ["{", name+"^{(%d)}(0)" % n, "\\over", "%d!" % n, "}", "x^%d" % n]


def taylorformula(n, name):
    l = []
    for i in range(n):
        l += formulaparts(i, name)
        l += ["+"]
    l += formulaparts(n, name)
    l += ["\cdots"]
    return l


class Scene1_intro(Scene):
    def construct(self):
        tar = FunctionGraph(
            np.sin, x_range=[-PI, PI, PI/20], stroke_color=WHITE)

        simple_poly_before_coif = VGroup(*[FunctionGraph(lambda t: t**(
            2*i+1), x_range=[-PI, PI, PI/20]) for i in range(5)]).arrange(RIGHT).set_width(14)
        before_label = VGroup(
            *[Tex("x^%d" % (2*i+1)).next_to(simple_poly_before_coif[i].get_center(), DOWN) for i in range(5)])

        self.play(*[ShowCreation(a) for a in simple_poly_before_coif],
                  *[Write(b) for b in before_label])
        self.wait()
        simple_poly = VGroup(*[FunctionGraph(func, x_range=[-PI, PI, PI/20])
                             for func in taylor_for_sin_partly(5)]).arrange(RIGHT).set_width(14)
        after_label = VGroup(*[Tex(coif, "x^%d" % (2*i+1)).next_to(simple_poly[i].get_center(), DOWN)
                             for i, coif in zip(range(5), ["", r"-\frac{1}{6}", r"\frac{1}{120}", r"-\frac{1}{5040}", r"\frac{1}{362880}"])])

        self.play(ReplacementTransform(simple_poly_before_coif, simple_poly), *
                  [TransformMatchingTex(a, b) for a, b in zip(before_label, after_label)])
        self.wait()
        poly = FunctionGraph(taylor_for_sin(5), x_range=[-PI, PI, PI/20])
        total = Tex("x^1", r"-\frac{1}{6}", "x^3", "+", r"\frac{1}{120}", "x^5",
                    r"-\frac{1}{5040}", "x^7", "+", r"\frac{1}{362880}", "x^9").next_to(tar, DOWN)
        self.play(LaggedStart(*[ReplacementTransform(a, poly) for a in simple_poly], lag_ratio=0.1),
                  TransformMatchingTex(VGroup(*[k for t in after_label for k in t]), total, group_type=VGroup))

        sins = Tex("\sin", " x", "=", "x^1", r"-\frac{1}{6}", "x^3", "+", r"\frac{1}{120}", "x^5",
                   r"-\frac{1}{5040}", "x^7", "+", r"\frac{1}{362880}", "x^9", "\cdots").next_to(tar, DOWN)

        self.play(ReplacementTransform(poly, tar),
                  TransformMatchingTex(total, sins))
        self.wait()
        title = TexText("泰勒级数").set_width(5)
        paths = [get_path(title, i) for i in range(4)]
        self.remove(tar)
        self.play(*[ReplacementTransform(tar.copy(), p)
                  for p in paths], FadeOut(sins))
        self.play(title.animate.set_fill(opacity=1))
        self.wait()
        self.play(FadeOut(title))
        self.wait()


class Scene2_Guide_to_3B1B(Scene):
    CONFIG = {
        "random_seed": None
    }

    def construct(self):
        text = TexText(
            r"\qquad\quad$\mathrm{3B1B}$有一期关于泰勒级数的视频，\\而本期要讲的是六个三角函数的泰勒级数，\\建议先看完$\mathrm{3B1B}$的视频再回来。").scale(.8)
        BV = Tex("\mathrm{BV1Gx411Y7cz}").shift(RIGHT*4).set_opacity(.5)

        thumb = ImageMobject("1629423930295", height=1.8*1.5).shift(RIGHT*4)
        pic_rec = VGroup(*[Rectangle(width=thumb.get_width(), height=1.8*1.5,
                         color=random_bright_color()).shift(RIGHT*4) for i in range(2)])
        for i, p in enumerate(pic_rec):
            if i % 2 == 0:
                p.reverse_points()
        text.next_to(pic_rec, LEFT)
        self.play(Write(text))
        self.play(FadeIn(thumb), FadeIn(BV))
        for i in range(6):
            self.wait(.5)
            self.bring_to_front(pic_rec[i % 2])
            self.play(ShowCreation(pic_rec[i % 2]))
        self.wait(.5)
        self.play(FadeOut(pic_rec), FadeOut(text), FadeOut(BV), FadeOut(thumb))
        self.wait()


class Scene3_Sine(Scene):
    def construct(self):
        sine = Tex("\sin (x)")
        d_1 = Tex("\cos (x)")
        d_2 = Tex("-\sin (x)")
        d_3 = Tex("-\cos (x)")
        sine.move_to(UL)
        d_1.move_to(UR)
        d_2.move_to(DR)
        d_3.move_to(DL)
        label = VGroup(sine, d_1, d_2, d_3)
        arrow = VGroup(*[Arc(start_angle=PI/6, angle=-PI/3, radius=2 **
                       0.5).add_tip().rotate_about_origin(-i*PI/2) for i in range(-1, 3)])
        dersine = VGroup(*[Tex(r"\frac{\mathrm{d}}{\mathrm{d}x}").scale(.5).next_to(
            k, dir) for k, dir in zip(arrow, [UP, RIGHT, DOWN, LEFT])])
        for i in range(4):
            self.play(Write(label[i]))
            self.wait(.5)
            self.play(ShowCreation(arrow[i]), Write(dersine[i]))
            self.wait(.5)
        subsitude = Tex("x=0")
        self.wait(.5)
        self.play(Write(subsitude))
        label2 = VGroup(Tex("\sin (0)").move_to(UL), Tex("\cos (0)").move_to(
            UR), Tex("-\sin (0)").move_to(DR), Tex("-\cos (0)").move_to(DL))
        self.wait()
        self.play(ReplacementTransform(label, label2))
        label3 = VGroup(Tex("0").move_to(UL), Tex("1").move_to(
            UR), Tex("0").move_to(DR), Tex("-1").move_to(DL))
        self.wait()
        self.play(ReplacementTransform(label2, label3))
        everything = VGroup(label3, arrow, subsitude, dersine)
        everything.save_state()
        self.wait()
        self.play(everything.animate.scale(.5).to_corner(UL))

        formula = Tex("\sin (x)", "=", *taylorformula(9, "\sin")).set_width(14)
        self.play(Write(formula))
        formula2list = ["\sin (x)", "=", *taylorformula(9, "\sin")]
        formula2list[2] = "0"
        formula2list[4+2-1] = "1"
        formula2list[9+2*2-1] = "0"
        formula2list[13+2*3-2] = "-"
        formula2list[14+2*3-1] = "1"
        formula2list[19+2*4-1] = "0"
        formula2list[24+2*5-1] = "1"
        formula2list[29+2*6-1] = "0"
        formula2list[33+2*7-2] = "-"
        formula2list[34+2*7-1] = "1"
        formula2list[39+2*8-1] = "0"
        formula2list[44+2*9-1] = "1"
        self.wait()
        formula2 = Tex(*formula2list).scale(.5)
        self.play(ReplacementTransform(formula, formula2))
        self.wait()
        formula3 = Tex("\sin (x)", "=", "x", "-", "{", "1", "\\over", "3!", "}", "x^3", "+", "{", "1", "\\over", "5!", "}", "x^5",
                       "-", "{", "1", "\\over", "7!", "}", "x^7", "+", "{", "1", "\\over", "9!", "}", "x^9", "\cdots")
        self.play(TransformMatchingTex(formula2, formula3))
        self.wait()
        formula4 = Tex("\sin (x)", "=", "\sum_{n=0}^\infty",
                       "(-1)^{2n+1}", "{", "1", "\\over", "(2n+1)!", "}", "x^{2n+1}")
        self.play(TransformMatchingTex(formula3, formula4), run_time=2)
        self.wait(3)
        self.play(FadeOut(formula4))
        self.wait()
        self.play(everything.animate.restore())
        self.wait()
        self.remove(*self.mobjects)


class test(Scene):
    def construct(self):
        a = Tex("a!", "+", "b!")
        b = Tex("c")
        self.play(TransformMatchingTex(a, b, key_map={"!": "c"}))


class Scene4_Cosine(Scene):
    def construct(self):
        label = VGroup(Tex("0").move_to(UL), Tex("1").move_to(
            UR), Tex("0").move_to(DR), Tex("-1").move_to(DL))
        arrow = VGroup(*[Arc(start_angle=PI/6, angle=-PI/3, radius=2 **
                       0.5).add_tip().rotate_about_origin(-i*PI/2) for i in range(-1, 3)])
        dersine = VGroup(*[Tex(r"\frac{\mathrm{d}}{\mathrm{d}x}").scale(.5).next_to(
            k, dir) for k, dir in zip(arrow, [UP, RIGHT, DOWN, LEFT])])
        x = Tex("x=0")
        everything = VGroup(label, arrow, dersine, x)
        rotation_tag = VGroup(Dot(UL), Dot(UR), Dot(DR),
                              Dot(DL)).set_opacity(0)

        def maintain_position(obj):
            for i, j in zip(obj, rotation_tag):
                i.move_to(j)

        label.add_updater(maintain_position)
        point_to_cos = Arrow(UR+UP*2, UR)
        label_cos = Tex("\cos(x)").next_to(point_to_cos, UP)
        point_to_cos.add_updater(lambda obj: obj.become(
            Arrow(rotation_tag[1].get_center()+UP*2, rotation_tag[1].get_center())))
        label_cos.add_updater(lambda obj: obj.next_to(point_to_cos, UP))
        self.add(*everything)
        self.play(Write(label_cos), Write(point_to_cos))
        self.wait()
        self.play(Rotating(VGroup(arrow, rotation_tag), angle=PI/2,
                  about_point=ORIGIN, rate_func=smooth), run_time=2)
        self.wait()
        point_to_cos.clear_updaters()
        label.clear_updaters()
        self.play(FadeOut(point_to_cos), FadeOut(label_cos))

        label.clear_updaters()
        self.wait()
        self.play(everything.animate.scale(.5).to_corner(UL))
        self.wait()
        formula = Tex("\cos(x)", "=", *taylorformula(9, "\cos")).set_width(14)
        formula2list = ["\cos(x)", "=", *taylorformula(9, "\cos")]
        formula2list[2] = "1"
        formula2list[4+2-1] = "0"
        formula2list[8+2*2-2] = "-"
        formula2list[9+2*2-1] = "1"
        formula2list[14+2*3-1] = "0"
        formula2list[19+2*4-1] = "1"
        formula2list[24+2*5-1] = "0"
        formula2list[28+2*6-2] = "-"
        formula2list[29+2*6-1] = "1"
        formula2list[34+2*7-1] = "0"
        formula2list[39+2*8-1] = "1"
        formula2list[44+2*9-1] = "0"
        formula2 = Tex(*formula2list).scale(.5)
        self.play(Write(formula))
        self.wait()
        self.play(ReplacementTransform(formula, formula2))
        self.wait()
        formula3 = Tex("\cos(x)", "=", "1", "-", "{", "1", "\\over", "2!", "}", "x^2", "+", "{", "1", "\\over", "4!", "}",
                       "x^4", "-", "{", "1", "\\over", "6!", "}", "x^6", "+", "{", "1", "\\over", "8!", "}", "x^8", "\\cdots")
        self.play(TransformMatchingTex(formula2, formula3))
        self.wait()
        formula4 = Tex("\cos(x)", "=", "\sum_{n=0}^\infty",
                       "(-1)^{2n}", "{", "1", "\\over", "(2n)!", "}", "x^{2n}")
        self.play(TransformMatchingTex(formula3, formula4), run_time=2)
        self.wait(3)
        self.play(FadeOut(formula4), FadeOut(everything))
        self.wait()


class Scene5_Tangent(Scene):
    def construct(self):
        formula1 = Tex("\\tan(x)", "=",
                       "{", "\sin(x)", "\\over", "\cos(x)", "}")
        tip = VGroup(Tex("\sin(x)=\\frac{e^{ix}-e^{-ix}}{2i}"), Tex(
            "\cos(x)=\\frac{e^{ix}+e^{-ix}}{2}")).scale(.5).arrange(DOWN).to_edge(UL)
        self.play(Write(formula1))
        self.wait()
        self.play(Write(tip))
        self.wait(2)
        formula2 = Tex(
            "\\tan(x)", "=", "{", "\\frac{e^{ix}-e^{-ix}}{2i}", "\\over", "\\frac{e^{ix}+e^{-ix}}{2}", "}")
        self.play(ReplacementTransform(formula1, formula2))
        self.wait()
        formula2p = Tex("\\tan(x)", "=", "{", "{", "e^{ix}-e^{-ix}", "\\over",
                        "2i}", "\\over", "{", " e^{ix}+e^{-ix}", "\\over", "2}", "}")
        self.remove(formula2)
        self.add(formula2p)
        formula3 = Tex(
            "\\tan(x)", "=", "{", "{", "e^{ix}-e^{-ix}", "\\over", " i}", "\\over", " e^{ix}+e^{-ix}", "}")
        self.play(TransformMatchingTex(formula2p, formula3))
        self.wait()
        formula4 = Tex("i", "\\tan(x)", "=",
                       "{", "e^{ix}-e^{-ix}", "\\over", " e^{ix}+e^{-ix}", "}")
        self.play(TransformMatchingTex(formula3, formula4))
        self.wait()
        multiply_up_and_down = VGroup(Tex("\\times e^{ix}").next_to(
            formula4[3]), Tex("\\times e^{ix}").next_to(formula4[5]))
        self.play(FadeIn(multiply_up_and_down, shift=LEFT))
        self.wait()
        self.play(FadeOut(multiply_up_and_down, shift=LEFT))
        self.wait()
        formula4p = Tex("i", "\\tan(x)", "=",
                        "{", "e^{ix}", "-", "e^{-ix}", "\\over", " e^{ix}", "+", "e^{-ix}", "}")
        self.add(formula4p)
        self.remove(formula4)
        formula5 = Tex("i", "\\tan(x)", "=",
                       "{", "e^{2ix}", "-", "1", "\\over", " e^{2ix}", "+", "1", "}")
        self.play(TransformMatchingTex(formula4p, formula5,
                  key_map={"e^{ix}": "e^{2ix}", "e^{-ix}": "1"}))
        self.wait()
        self.play(formula5.animate.scale(.5).to_edge(UL), FadeOut(tip))
        self.wait()
        subtitle = TexText("我们先来看看伯努利数的生成函数定义式")
        self.play(Write(subtitle))
        self.wait()
        self.play(FadeOut(subtitle))
        definition = Tex(
            "f(x)", "=", "\\sum_{n=0}^\infty", " B_n", "\\frac{x^n}{n!}", "=", "\\frac{x}{e^x-1}")
        self.play(Write(definition))
        self.wait()
        bound = SurroundingRectangle(definition[3])
        没啥用的 = TexText("第$n$个伯努利数").next_to(
            definition[3], DOWN, buff=1.5).scale(.5)
        point_to_b = Arrow(没啥用的.get_center(), bound.get_center(), buff=0.5)
        Bs = VGroup(
            Tex("B_0=1"), Tex(
                "B_1=-\\frac12"), Tex("B_2=\\frac16"), Tex("B_3=0"), Tex("B_4=-\\frac{1}{30}"),
            Tex("B_5=0"), Tex("B_6=\\frac{5}{66}"), Tex(
                "B_7=0"), Tex("B_8=-\\frac{691}{2730}"), Tex("B_9=0"),
            Tex("B_{10}=\\frac76"), Tex("B_{11}=0"), Tex(
                "B_{12}=-\\frac{3617}{510}"), Tex("B_{13}=0"), Tex("B_{14}=\\frac{43867}{798}")
        ).scale(.5).arrange_in_grid(3, 5).to_edge(DOWN)
        self.play(ShowCreation(bound), Write(point_to_b), Write(没啥用的))
        self.wait()
        bound.reverse_points()
        self.play(Uncreate(bound), FadeOut(point_to_b), FadeOut(没啥用的))
        self.wait()
        self.play(Write(Bs), run_time=5)
        self.wait(3)
        self.play(LaggedStart(FadeOut(Bs, shift=DOWN)))
        self.wait()
        self.play(definition.animate.scale(.5).next_to(
            formula5, DOWN).align_to(formula5, LEFT))
        self.wait()
        gx = Tex("g(x)", "=", "{", "f(x)", "-", "f(-x)", "\\over", " x", "}")
        self.play(Write(gx))
        self.wait()
        gx2 = Tex("g(x)", "=", "{", "\\frac{x}{e^x-1}",
                  "-", "\\frac{-x}{e^{-x}-1}", "\\over", " x", "}")
        self.play(TransformMatchingTex(gx, gx2, key_map={
                  "f(x)": "\\frac{x}{e^x-1}", "f(-x)": "\\frac{-x}{e^{-x}-1}"}))
        self.wait()
        gx3 = Tex("g(x)", "=", "\\frac{1}{e^x-1}", "+", "\\frac{-1}{e^{-x}-1}")
        self.play(TransformMatchingTex(gx2, gx3, key_map={
                  "\\frac{x}{e^x-1}": "\\frac{1}{e^x-1}", "\\frac{-x}{e^{-x}-1}": "\\frac{-1}{e^{-x}-1}"}))
        self.wait()
        gx3p = Tex("g(x)", "=", "{", "1", "\\over", " e^x", "-", "1", "}",
                   "+", "{", "-1", "\\over", " e^{-x}", "-", "{1}", "}")
        self.add(gx3p)
        self.remove(gx3)
        gx4 = Tex("g(x)", "=", "{", "1", "\\over", " e^x", "-", "1", "}",
                  "+", "{", "{1}", "\\over", "{1}", "-", "e^{-x}", "}")
        self.play(TransformMatchingTex(gx3p, gx4, key_map={"-1": "{1}"}))
        self.wait()
        gx5 = Tex("g(x)", "=", "{", "1", "\\over", " e^x", "-", "1", "}",
                  "+", "{", "e^{x}", "\\over", " e^{x}", "-", "{1}", "}")
        self.play(TransformMatchingTex(gx4, gx5),key_map={"{1}":"e^{x}","e^{-x}":"{1}"})
        self.wait()
        gx6 = Tex("g(x)", "=", "{", "e^{x}", "+", "1",
                  "\\over", " e^x", "-", "1", "}")
        self.play(TransformMatchingTex(gx5, gx6))
        self.wait()
        
        def gxTex(*texs):
            return Tex("g(x)", "=", *texs, "=", "{", "e^{x}", "+", "1", "\\over", " e^x", "-", "1", "}")
        gx7 = gxTex("{", "\sum\limits_{n=0}^\infty", " B_n", "{", "x^n", "\\over", " n!", "}", "+",
                    "\sum\limits_{n=0}^\infty", " B_n", "{", "(-x)^n", "\\over", " n!", "}", "\\over x", "}")
        self.play(TransformMatchingTex(gx6, gx7))
        self.wait()
        discribe = TexText("当$n$为奇数时，两者符号相反，正好相消").to_edge(
            UP, buff=1).scale(.7)
        discribe2 = TexText("当$n$为偶数时，两者符号相同，正好叠加").to_edge(
            UP, buff=1).scale(.7)
        indentify = Arrow(
            np.array([gx7[4].get_x(), discribe.get_y(), 0]), gx7[4].get_center(), buff=.5)
        indentify2 = Arrow(np.array(
            [gx7[10].get_x(), discribe.get_y(), 0]), gx7[10].get_center(), buff=.5)
        bound1 = SurroundingRectangle(gx7[4:7])
        bound2 = SurroundingRectangle(gx7[10:13])
        self.play(ShowCreation(bound1), ShowCreation(
            bound2), Write(indentify), Write(indentify2))
        self.play(Write(discribe))
        self.wait(3)
        self.play(Write(discribe2), FadeOut(discribe))
        self.wait(3)
        bound1.reverse_points()
        bound2.reverse_points()
        self.play(FadeOut(VGroup(discribe2, indentify2, indentify)),
                  Uncreate(bound1), Uncreate(bound2))
        self.wait()
        gx8 = gxTex("{", "2", "\sum\limits_{n=0}^\infty", " B_{2n}",
                    "{", "x^{2n}", "\\over", "(2n)!", "}", "\\over x", "}")
        self.play(TransformMatchingTex(gx7, gx8, key_map={
                  "x^n": "x^{2n}", "n!": "(2n)!", "B_n": "B_{2n}"}))
        self.wait()
        gx9 = gxTex("2", "\sum\limits_{n=0}^\infty",
                    " B_{2n}", "{", "x^{2n-1}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(
            gx8, gx9, key_map={"x^{2n}": "x^{2n-1}"}))
        self.wait()
        self.play(gx9.animate.scale(.5).next_to(
            definition, DOWN).align_to(formula5, LEFT))
        hx = Tex("h(x)", "=", "2g(2x)-g(x)")
        self.wait()
        self.play(Write(hx))
        self.wait()
        hx2 = Tex("h(x)", "=", "2g(2x)-g(x)", "=",
                  "\\frac{e^x-1}{e^x+1}", "=", "{", "1", "\\over", " g(x)", "}")
        homework = TexText("这个推导很简单哦，可以来试一试！",
                           color=GREY).scale(.7).next_to(hx2, DOWN)
        self.play(TransformMatchingTex(hx, hx2))
        self.wait()
        self.play(Write(homework))
        self.wait()
        self.play(FadeOut(homework), hx2.animate.scale(.5).next_to(
            gx9, DOWN).align_to(formula5, LEFT))
        self.wait()
        tanx = Tex("i", "\\tan(x)", "=",
                   "{", "e^{2ix}", "-", "1", "\\over", " e^{2ix}", "+", "1", "}")
        self.play(ReplacementTransform(formula5.copy(), tanx))
        self.wait()
        back = SurroundingRectangle(
            VGroup(formula5, hx2), stroke_width=0, fill_color=BLACK, fill_opacity=0.5)
        tanx2 = Tex("i", "\\tan(x)", "=", "h", "(", "2", "i", "x", ")")
        self.play(FlashAround(hx2))
        self.wait()
        self.play(TransformMatchingTex(tanx, tanx2))
        self.wait()
        tanx3 = Tex("i", "\\tan(x)", "=", "2", "g", "(", "4", "i",
                    "x", ")", "-", "g", "(", "2", "i", "x", ")")
        self.play(FlashAround(hx2))
        self.wait()
        self.play(TransformMatchingTex(tanx2, tanx3), FadeIn(back))
        self.wait()
        tanx3p = Tex("i", "\\tan(x)", "=", "2", "g(4ix)", "-", "g(2ix)")
        self.add(tanx3p)
        self.remove(tanx3)
        tanx4p = Tex(
            "i", "\\tan(x)", "=",
            "2", "\cdot",
            "2\sum\limits_{n=0}^\infty B_{2n}{(4ix)^{2n-1}\\over(2n)!}",
            "-",
            "2\sum\limits_{n=0}^\infty B_{2n}{(2ix)^{2n-1}\\over(2n)!}"
        )
        self.play(TransformMatchingTex(tanx3p, tanx4p,
                                       key_map={
                                           "g(4ix)": "2\sum\limits_{n=0}^\infty B_{2n}{(4ix)^{2n-1}\\over(2n)!}",
                                           "g(2ix)": "2\sum\limits_{n=0}^\infty B_{2n}{(2ix)^{2n-1}\\over(2n)!}"
                                       }))
        self.wait()
        tanx4 = Tex(
            "i", "\\tan(x)", "=",
            "2", "\cdot",
            "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "(4ix)^{2n-1}", "\\over", "(2n)!", "}",
            "-",
            "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "(2ix)^{2n-1}", "\\over", "(2n)!", "}"
        )
        self.add(tanx4)
        self.remove(tanx4p)
        tanx5 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "4", "(4ix)^{2n-1}", "\\over", "(2n)!", "}",
            "-",
            "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "2", "(2ix)^{2n-1}", "\\over", "(2n)!", "}"
        )
        self.play(TransformMatchingTex(tanx4, tanx5))
        self.wait()
        tanx6 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "\left[", " B_{2n}", "{", "4", "(4ix)^{2n-1}", "\\over", "(2n)!", "}",
            "-",
            " B_{2n}", "{", "2", "(2ix)^{2n-1}", "\\over", "(2n)!", "}", "\\right]"
        )
        self.play(TransformMatchingTex(tanx5, tanx6))
        self.wait()
        tanx7 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "\left[", "4", "(4ix)^{2n-1}",
            "-",
            "2", "(2ix)^{2n-1}", "\\right]"
        )
        self.play(TransformMatchingTex(tanx6, tanx7))
        self.wait()
        tanx7p = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "\left[",
            "4", "(", "4", "i", "x", ")", "^{2n-1}",
            "-",
            "2", "(", "2", "i", "x", ")", "^{2n-1}", "\\right]"
        )
        self.add(tanx7p)
        self.remove(tanx7)
        tanx8 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "\left[",
            "4", "\cdot",  "4", "^{2n-1}", "i", "^{2n-1}",
            "-",
            "2", "\cdot", "2", "^{2n-1}", "i", "^{2n-1}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx7p, tanx8))
        self.wait()
        tanx8p = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "\left[",
            "4", "\cdot",  "4", "^{", "2n", "-1", "}", "i", "^{", "2", "n", "-", "1", "}"
            "-",
            "2", "\cdot", "2", "^{", "2n", "-1", "}", "i", "^{", "2", "n", "-", "1", "}", "\\right]", "x", "^{2n-1}"
        )
        self.add(tanx8p)
        self.remove(tanx8)
        tanx9 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "\left[",
            "4", "^{", "2n", "}", "i", "^{", "2", "n", "-", "1", "}"
            "-",
            "2", "^{", "2n", "}", "i", "^{", "2", "n", "-", "1", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx8p, tanx9))
        self.wait()
        tanx10 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "i", "^{", "2", "n", "-", "1", "}",
            "\left[",
            "4", "^{", "2n", "}", "-", "2", "^{", "2n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx9, tanx10))
        self.wait()
        tanx11 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "i", "^{", "2", "n", "}", "i", "^{", "-", "1", "}",
            "\left[",
            "4", "^{", "2n", "}", "-", "2", "^{", "2n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx10, tanx11))
        self.wait()
        tanx11p = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", r"{i}", "^{", "2", "n", "}", r"{{i}}", "^{", "-", "1", "}",
            "\left[",
            "4", "^{", "2n", "}", "-", "2", "^{", "2n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.add(tanx11p)
        self.remove(tanx11)
        tanx12 = Tex(
            "i", "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-1)", "^{", "n", "}", "(-i)"
            "\left[",
            "4", "^{", "2n", "}", "-", "2", "^{", "2n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx11p, tanx12,
                  key_map={r"{i}": "(-1)", r"{{i}}": "(-i)"}))
        self.wait()
        tanx13 = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-1)", "^{", "n", "}"
            "\left[",
            "2", "^{", "2n", "}", "-", "4", "^{", "2n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx12, tanx13))
        self.wait()
        tanx13p = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-1)", "^{", "n", "}"
            "\left[",
            "2", "^{", "2", "n", "}", "-", "4", "^{", "2", "n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.add(tanx13p)
        self.remove(tanx13)
        tanx14 = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-1)", "^{", "n", "}"
            "\left[",
            "4", "^{", "n", "}", "-", "4", "^{", "2", "n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx13p, tanx14))
        self.wait()
        tanx15 = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-1)", "^{", "n", "}", "4", "^{", "n", "}"
            "\left[",
            "1", "-", "4", "^{", "n", "}", "\\right]", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx14, tanx15))
        self.wait()
        tanx16 = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=0}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-4)", "^{", "n", "}"
            "(",
            "1", "-", "4", "^{", "n", "}", ")", "x", "^{2n-1}"
        )
        self.play(TransformMatchingTex(tanx15, tanx16, kay_map={
                  "(-1)": "(-4)", "\left[": "(", "\\right]": ")"}))
        self.wait()
        n = SurroundingRectangle(tanx16[8:14])
        text = TexText("当$n=0$时，这一项为$0$").scale(.7).to_edge(DOWN, buff=1)
        point_to = Arrow(
            np.array([n.get_x(), text.get_y(), 0]), n.get_center(), buff=0.5)
        text.shift(n.get_x()*RIGHT)
        self.play(ShowCreation(n), Write(point_to))
        self.play(Write(text))
        self.wait(3)
        n.reverse_points()
        self.play(Uncreate(n), FadeOut(text), FadeOut(point_to))
        tanx17 = Tex(
            "\\tan(x)", "=",
            "\sum\limits_{n=1}^\infty", "{", "B_{2n}", "\\over", "(2n)!", "}", "(-4)", "^{", "n", "}"
            "(",
            "1", "-", "4", "^{", "n", "}", ")", "x", "^{2n-1}"
        )
        self.wait()
        self.play(ReplacementTransform(tanx16, tanx17))
        self.wait(3)
        self.play(FadeOut(tanx17),FadeOut(back))
        self.wait(2)
        self.remove(*self.mobjects)


class Scene6_Cotangent(Scene):
    def construct(self):
        def gxTex(*texs):
            return Tex("g(x)", "=", *texs, "=", "{", "e^{x}", "+", "1", "\\over", " e^x", "-", "1", "}")
        tanx = Tex("i", "\\tan(x)", "=", "{", "e^{2ix}", "-", "1",
                   "\\over", " e^{2ix}", "+", "1", "}").scale(.5).to_corner(UL)
        fx = Tex("f(x)", "=", "\\sum_{n=0}^\infty", " B_n", "\\frac{x^n}{n!}", "=",
                 "\\frac{x}{e^x-1}").scale(.5).next_to(tanx, DOWN).align_to(tanx, LEFT)
        gx = gxTex("2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "x^{2n-1}",
                   "\\over", "(2n)!", "}").scale(.5).next_to(fx, DOWN).align_to(tanx, LEFT)
        hx = Tex("h(x)", "=", "2g(2x)-g(x)", "=", "\\frac{e^x-1}{e^x+1}", "=", "{",
                 "1", "\\over", " g(x)", "}").scale(.5).next_to(gx, DOWN).align_to(tanx, LEFT)
        self.add(tanx, fx, gx, hx)
        back = SurroundingRectangle(
            VGroup(tanx, hx), stroke_width=0, fill_color=BLACK, fill_opacity=.5)
        cotx = Tex("-", "i", "\cot(x)", "=", "{", "e^{2ix}", "+", "1",
                   "\\over", " e^{2ix}", "-", "1", "}").scale(.5).to_corner(UL)
        text = TexText("两边同时取倒数").set_color(GREY).next_to(cotx)
        self.play(Write(text))
        self.wait()
        self.play(TransformMatchingTex(
            tanx, cotx, key_map={"\\tan(x)": "\cot(x)"}))
        self.wait()
        self.play(FadeOut(text))
        self.wait()
        cotxp = Tex("-", "i", "\cot(x)", "=",
                    "{", "e^{2ix}", "+", "1", "\\over", " e^{2ix}", "-", "1", "}")
        self.play(ReplacementTransform(cotx.copy(), cotxp))
        self.wait()
        self.play(FadeIn(back))
        self.wait()
        cotx2 = Tex("-", "i", "\cot(x)", "=", "g(2ix)")
        self.play(TransformMatchingTex(cotxp, cotx2))
        self.wait()
        cotx3 = Tex("-", "i", "\cot(x)", "=",
                    "2\sum\limits_{n=0}^\infty B_{2n}{(2ix)^{2n-1}\\over(2n)!}")
        cotx3p = Tex("-", "i", "\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}",
                     "{", "(", "2", "i", "x", ")", "^{2", "n", "-", "1}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx2, cotx3, key_map={
                  "g(2ix)": "2\sum\limits_{n=0}^\infty B_{2n}{(2ix)^{2n-1}\\over(2n)!}"}))
        self.wait()
        self.remove(cotx3)
        cotx4 = Tex("-", "i", "\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "2", "^{2",
                    "n", "-", "1}", "i", "^{2", "n", "-", "1}", "x", "^{2", "n", "-", "1}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx3p, cotx4))
        self.wait()
        cotx5 = Tex("\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "2", "^{2",
                    "n", "-", "1}", "i", "^{2", "n", "}", "x", "^{2", "n", "-", "1}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx4, cotx5))
        self.wait()
        cotx6 = Tex("\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "2", "^{2", "n",
                    "-", "1}", "(-1)", "^{", "n", "}", "x", "^{2", "n", "-", "1}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx5, cotx6, key_map={"i": "(-1)"}))
        self.wait()
        cotx7 = Tex("\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "2", "^{", "2",
                    "n", "}", "(-1)", "^{", "n", "}", "x", "^{2", "n", "-", "1}", "\\over", "2", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx6, cotx7))
        self.wait()
        cotx8 = Tex("\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{", "4", "^{", "n",
                    "}", "(-1)", "^{", "n", "}", "x", "^{2", "n", "-", "1}", "\\over", "2", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx7, cotx8))
        self.wait()
        cotx9 = Tex("\cot(x)", "=", "2", "\sum\limits_{n=0}^\infty", " B_{2n}", "{",
                    "(-4)", "^{", "n", "}", "x", "^{2", "n", "}", "\\over", "2", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx8, cotx9))
        self.wait()
        cotx10 = Tex("\cot(x)", "=", "\sum\limits_{n=0}^\infty", " B_{2n}",
                     "{", "(-4)", "^{", "n", "}", "x", "^{2", "n", "}", "\\over", "(2n)!", "}")
        self.play(TransformMatchingTex(cotx9, cotx10))
        self.wait(3)
        self.play(FadeOut(cotx10), FadeOut(back))
        self.wait()
        self.play(FadeOut(VGroup(fx, gx, hx, cotx)))
        self.wait(2)


class Scene7_Secent(Scene):
    def construct(self):
        intro = TexText("我们再来看看欧拉数的定义")
        self.play(Write(intro))
        self.wait(3)
        self.play(FadeOut(intro))
        E = Tex("\sech(x)", "=", "\sum_{n=0}^\infty", "{",
                "(-1)^n", "\\over", "(2n)!", "}", "E_n", "x^{2n}")
        self.play(Write(E))
        self.wait()
        Ebound = SurroundingRectangle(E[6])
        label = TexText("第$n$个欧拉数").move_to(E[6]).shift(DOWN*2).scale(.7)
        point_to_E = Arrow(label.get_center(), Ebound.get_center(), buff=0.5)
        Es = VGroup(*[Tex("E_{%d}=%d" % (i, j)) for i, j in zip(range(15),
                    e[:15])]).scale(.5).arrange_in_grid(5, 3).to_edge(DOWN)
        self.play(ShowCreation(Ebound), Write(point_to_E))
        self.wait()
        self.play(Write(label))
        self.wait(3)
        Ebound.reverse_points()
        self.play(FadeOut(label), FadeOut(point_to_E), Uncreate(Ebound))
        self.wait()
        self.play(Write(Es), run_time=5)
        self.wait(3)
        self.play(LaggedStart(FadeOut(Es, shift=DOWN)))
        E2 = Tex("\sech(x)", "=", "\\frac{2}{e^x+e^{-x} }", "=", "\sum_{n=0}^\infty",
                 "{", "(-1)^n", "\\over", "(2n)!", "}", "E_n", "x^{2n}")
        self.play(TransformMatchingTex(E, E2))
        self.wait()
        self.play(E2.animate.scale(.5).to_corner(UL))
        self.wait()
        secx = Tex("\sec(x)", "=", "{", "1", "\\over", "\cos(x)", "}")
        self.play(Write(secx))
        self.wait()
        secx2 = Tex("\sec(x)", "=",
                    "{", "1", "\\over", "\\frac{e^{ix}+e^{-ix}}{2}", "}")
        self.play(TransformMatchingTex(secx, secx2, key_map={
                  "\cos(x)": "\\frac{e^{ix}+e^{-ix}}{2}"}))
        self.wait()
        secx2p = Tex("\sec(x)", "=", "{", "1", "\\over",
                     "{", "e^{ix}+e^{-ix}", "\\over", "2", "}", "}")
        self.remove(secx2)
        self.add(secx2p)
        secx3 = Tex("\sec(x)", "=", "{", "2", "\\over", " e^{ix}+e^{-ix}", "}")
        self.play(TransformMatchingTex(secx2p, secx3))
        self.wait()
        self.play(*[ShowCreationThenDestructionAround(i) for i in [E2[2]
                  [3], E2[2][7:], secx3[-1][1:3], secx3[-1][5:]]], run_time=2)
        self.wait()
        secx3p = Tex("\sec(x)", "=", "{2\\over e^{ix}+e^{-ix} }")
        secx4 = Tex("\sec(x)", "=", "\sech(ix)")
        self.remove(secx3)
        self.add(secx3p)
        self.play(TransformMatchingTex(secx3p, secx4, key_map={
                  "{2\\over e^{ix}+e^{-ix} }": "\sech(x)"}))
        self.wait()
        secx5 = Tex("\sec(x)", "=", "\sum_{n=0}^\infty", "{", "(-1)^n",
                    "\\over", "(2n)!", "}", "E_n", "(", "i", "x", ")", "^{2", "n", "}")
        secx5p = Tex("\sec(x)", "=",
                     "\sum_{n=0}^\infty{(-1)^n\\over(2n)!}E_n(ix)^{2n}")
        self.play(TransformMatchingTex(secx4, secx5p, key_map={
                  "\sech(x)": "\sum_{n=0}^\infty{(-1)^n\\over(2n)!}E_n(ix)^{2n}"}))
        self.wait()
        self.remove(secx5p)
        self.add(secx5)
        secx6 = Tex("\sec(x)", "=", "\sum_{n=0}^\infty", "{", "(-1)^n", "\\over",
                    "(2n)!", "}", "E_n", "i", "^{2", "n", "}", "x", "^{2", "n", "}")
        self.play(TransformMatchingTex(secx5, secx6))
        self.wait()
        secx7 = Tex("\sec(x)", "=", "\sum_{n=0}^\infty", "{", "(-1)^n", "\\over",
                    "(2n)!", "}", "E_n", "(-1)", "^{", "n", "}", "x", "^{2", "n", "}")
        self.play(TransformMatchingTex(secx6, secx7, key_map={"i": "(-1)"}))
        self.wait()
        secx7p = Tex("\sec(x)", "=", "\sum_{n=0}^\infty", "{", "(-1)^n",
                     "\\over", "(2n)!", "}", "E_n", "(-1)^n", "x^{2n}")
        self.add(secx7p)
        self.remove(secx7)
        secx8 = Tex("\sec(x)", "=", "\sum_{n=0}^\infty",
                    "{", "1", "\\over", "(2n)!", "}", "E_n", "x^{2n}")
        self.play(TransformMatchingTex(secx7p, secx8, key_map={"(-1)^n": "1"}))
        self.wait()
        secx9 = Tex("\sec(x)", "=", "\sum_{n=0}^\infty",
                    "{", "E_n", "\\over", "(2n)!", "}", "x^{2n}")
        self.play(TransformMatchingTex(secx8, secx9))
        self.wait(3)
        self.play(FadeOut(secx9), FadeOut(E2))
        self.wait(2)


class Scene8_Cosecent(Scene):
    def construct(self):
        fx = Tex("f(x)", "=", "\\sum_{n=0}^\infty", " B_n", "\\frac{x^n}{n!}", "=",
                 "\\frac{x}{e^x-1}").scale(.5).to_edge(UL)
        self.play(FadeIn(fx))
        self.wait()
        csc = Tex("\csc(x)", "=", "{", "1", "\\over", "\sin(x)", "}")
        self.play(Write(csc))
        self.wait()
        csc2 = Tex("\csc(x)", "=", "{", "1", "\\over",
                   "\\frac{e^{ix}-e^{-ix}}{2i}", "}")
        csc2p = Tex("\csc(x)", "=", "{", "1", "\\over",
                    "{", "e^{ix}-e^{-ix}", "\\over", "2", "i", "}", "}")
        self.play(TransformMatchingTex(csc, csc2, key_map={
                  "\sin(x)": "\\frac{e^{ix}-e^{-ix}}{2i}"}))
        self.wait()
        self.remove(csc2)
        self.add(csc2p)
        csc3 = Tex("\csc(x)", "=", "{", "2", "i",
                   "\\over", " e^{ix}-e^{-ix}", "}")
        self.play(TransformMatchingTex(csc2p, csc3))
        self.wait()
        csc4 = Tex("\csc(x)", "=", "{", "2", "i", "e^{ix}",
                   "\\over", "(", "e^{ix}-e^{-ix}", ")", "e^{ix}", "}")
        self.play(TransformMatchingTex(csc3, csc4))
        self.wait()
        csc5 = Tex("\csc(x)", "=", "{", "2", "i",
                   "e^{ix}", "\\over", " e^{2ix}-1", "}")
        self.play(TransformMatchingTex(
            csc4, csc5, key_map={"e^{ix}-e^{-ix}": "e^{2ix}-1"}))
        self.wait()
        csc6 = Tex("\csc(x)", "=", "2", "i",
                   "{", "e^{ix}", "+1", "-", "1", "\\over", " e^{2ix}-1", "}")
        self.play(TransformMatchingTex(csc5, csc6))
        self.wait()
        csc7 = Tex("\csc(x)", "=", "2", "i", "\left(", "{", "e^{ix}", "+1", "\\over",
                   " e^{2ix}-1", "}", "-", "{", "1", "\\over", " e^{2ix}-1", "}", "\\right)")
        self.play(TransformMatchingTex(csc6, csc7))
        self.wait()
        csc8 = Tex("\csc(x)", "=", "2", "i", "\left(",
                   "{", "1", "\\over", " e^{ix}-1", "}", "-", "{", "1", "\\over", " e^{2ix}-1", "}", "\\right)")
        self.play(TransformMatchingTex(csc7, csc8))
        self.wait()
        csc8p = Tex("\csc(x)", "=", "2", "\left(",
                    "{", "i", "\\over", " e^{ix}-1", "}", "-", "{", "i", "\\over", " e^{2ix}-1", "}", "\\right)")
        self.play(TransformMatchingTex(csc8, csc8p))
        self.wait()
        csc9 = Tex("\csc(x)", "=", "{", "2", "\\over", " x", "}", "\left(", "{", "i", "x", "\\over",
                   " e^{ix}-1", "}", "-", "\\frac12", "{", "2", "i", "x", "\\over", " e^{2ix}-1", "}", "\\right)")
        self.play(TransformMatchingTex(csc8p, csc9))
        self.wait()
        csc10p = Tex(
            "\csc(x)", "=", "{", "2", "\\over", " x", "}", "\left(",
            "\sum_{n=0}^\infty B_n{(ix)^n\\over n!}",
            "-", "\\frac12",
            "\sum_{n=0}^\infty B_n{(2ix)^n\\over n!}",
            "\\right)")
        csc10 = Tex(
            "\csc(x)", "=", "{", "2", "\\over", " x", "}", "\left(",
            "\sum_{n=0}^\infty", " B_n", "{", "(", "i", "x", ")", "^n", "\\over", " n!", "}",
            "-", "{", "1", "\\over", "2", "}",
            "\sum_{n=0}^\infty", " B_n", "{", "(", "2", "i", "x", ")", "^n", "\\over", " n!", "}",
            "\\right)")
        self.play(TransformMatchingTex(csc9, csc10p))
        self.wait()
        self.remove(csc10p)
        self.add(csc10)
        csc11 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}", "\left(", "2",
            "\sum_{n=0}^\infty", " B_n", "{", "(", "i", "x", ")", "^n", "\\over", " n!", "}",
            "-",
            "\sum_{n=0}^\infty", " B_n", "{", "(", "2", "i", "x", ")", "^n", "\\over", " n!", "}",
            "\\right)")
        self.play(TransformMatchingTex(csc10, csc11))
        self.wait()
        csc12 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}", "\left(",
            "\sum_{n=0}^\infty", "2", " B_n", "{", "i", "^n", "x", "^n", "\\over", " n!", "}",
            "-",
            "\sum_{n=0}^\infty", " B_n", "{", "2", "^n", "i", "^n", "x", "^n", "\\over", " n!", "}",
            "\\right)")
        self.play(TransformMatchingTex(csc11, csc12))
        self.wait()
        csc13 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "\\left(", "2", " B_n", "{", "i", "^n", "x", "^n", "\\over", " n!", "}",
            "-",
            " B_n", "{", "2", "^n", "i", "^n", "x", "^n", "\\over", " n!", "}", "\\right)")
        self.play(TransformMatchingTex(csc12, csc13))
        self.wait()
        csc14 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "{", "B_n", "\\over", " n!", "}", "i", "^n", "\\left(", "2",
            "-",
            "2", "^n", "\\right)", "x", "^n"
        )
        self.play(TransformMatchingTex(csc13, csc14))
        self.wait()
        Bs = VGroup(
            Tex("B_0=1"), Tex(
                "B_1=-\\frac12"), Tex("B_2=\\frac16"), Tex("B_3=0"), Tex("B_4=-\\frac{1}{30}"),
            Tex("B_5=0"), Tex("B_6=\\frac{5}{66}"), Tex(
                "B_7=0"), Tex("B_8=-\\frac{691}{2730}"), Tex("B_9=0"),
            Tex("B_{10}=\\frac76"), Tex("B_{11}=0"), Tex(
                "B_{12}=-\\frac{3617}{510}"), Tex("B_{13}=0"), Tex("B_{14}=\\frac{43867}{798}")
        ).scale(.5).arrange_in_grid(3, 5).to_edge(DOWN)
        self.play(Write(Bs))
        self.wait()
        self.play(LaggedStart(
            *[ShowCreationThenDestructionAround(Bs[i]) for i in np.arange(3, 15, 2)]))
        text1 = TexText("当$n$为$>1$的奇数时，系数为$0$").scale(.7).to_edge(UP)
        text2 = TexText("当$n$为1时，系数为$0$").scale(.7).next_to(text1, DOWN)
        text3 = TexText("所以，当$n$为奇数时，系数为$0$，可以省略").scale(.7).to_edge(UP)
        coif1 = SurroundingRectangle(csc14[6])
        coif2 = SurroundingRectangle(csc14[11:-2])
        coif3 = SurroundingRectangle(csc14[6:-2])
        point1 = Arrow(text1.get_center(), coif1.get_center(), buff=0.5)
        point2 = Arrow(text2.get_center(), coif2.get_center(), buff=0.5)
        point3 = Arrow(text3.get_center(), coif3.get_center(), buff=0.5)
        self.play(ShowCreation(coif1), Write(point1))
        self.wait()
        self.play(Write(text1))
        self.wait(3)
        self.play(Write(text2), ReplacementTransform(
            point1, point2), ReplacementTransform(coif1, coif2))
        self.wait(3)
        self.play(FadeOut(text1, shift=DOWN), FadeOut(text2, shift=DOWN), Write(
            text3), ReplacementTransform(point2, point3), ReplacementTransform(coif2, coif3))
        self.wait(3)
        self.play(FadeOut(point3), FadeOut(coif3), FadeOut(text3), FadeOut(Bs))
        self.wait()
        csc15 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "{", "B_{2n}", "\\over", " (2n)!", "}", "i", "^{2n}", "\\left(", "2",
            "-",
            "2", "^{2n}", "\\right)", "x", "^{2n}"
        )
        self.play(TransformMatchingTex(csc14, csc15, key_map={
                  "^n": "^{2n}", "B_n": "B_{2n}", "n!": "(2n)!"}))
        self.wait()
        csc15p = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "{", "B_{2n}", "\\over", " (2n)!", "}", "i", "^{2", "n}", "\\left(", "2",
            "-",
            "2", "^{2", "n}", "\\right)", "x", "^{2", "n}"
        )
        self.add(csc15p)
        self.remove(csc15)
        csc16 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "{", "B_{2n}", "\\over", " (2n)!", "}", "(-1)", "^{", "n}", "\\left(", "2",
            "-",
            "2", "^{2", "n}", "\\right)", "x", "^{2", "n}"
        )
        self.play(TransformMatchingTex(csc15p, csc16, key_map={"i": "(-1)"}))
        self.wait()
        csc16p = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "{", "B_{2n}", "\\over", " (2n)!", "}", "(-1)^n", "\\left(", "2",
            "-",
            "2", "^{2", "n", "}", "\\right)", "x", "^{2", "n}"
        )
        self.add(csc16p)
        self.remove(csc16)
        csc17 = Tex(
            "\csc(x)", "=", "{", "1", "\\over", " x", "}",
            "\sum_{n=0}^\infty", "2", "{", "B_{2n}", "\\over", " (2n)!", "}", "(-1)^n", "\\left(", "1",
            "-",
            "2", "^{2", "n", "-", "1", "}", "\\right)", "x", "^{2", "n}"
        )
        self.play(TransformMatchingTex(csc16p, csc17))
        self.wait()
        csc18 = Tex(
            "\csc(x)", "=",
            "\sum_{n=0}^\infty", "2", "{", "B_{2n}", "\\over", " (2n)!", "}", "(-1)^n", "\\left(", "1",
            "-",
            "2", "^{2", "n", "-", "1", "}", "\\right)", "x", "^{2","{", "n}","-1}"
        )
        self.play(TransformMatchingTex(csc17,csc18))
        self.wait(3)
        self.play(FadeOut(csc18))
        self.wait()
        self.play(FadeOut(fx))
        self.wait(2)


class taylor(Scene):
    def construct(self):
        target = VGroup(*[FunctionGraph(func, x_range=rang) for func, rang in [(np.sin, [-2.5*PI, 2.5*PI, PI/20]), (np.cos, [-2.5*PI, 2.5*PI, PI/20]), (np.tan,[-PI/2+PI/40, PI/2-PI/40, PI/80]), (lambda x:1/np.tan(x), [PI/40, PI-PI/40, PI/80]), (lambda x:1/np.cos(x), [-PI/2+PI/40, PI/2-PI/40, PI/80]), (lambda x:1/np.sin(x), [PI/40, PI-PI/40, PI/80])]]).set_color(WHITE)
        taylor_list = [taylor_for_sin, taylor_for_cos, taylor_for_tan,
                       taylor_for_cot, taylor_for_sec, taylor_for_csc]
        ranges = [[-2.5*PI, 2.5*PI, PI/20], [-2.5*PI, 2.5*PI, PI/20], [-PI/2+PI/40, PI/2-PI/40, PI/80],
                  [PI/40, PI-PI/40, PI/80], [-PI/2+PI/40, PI/2-PI/40, PI/80], [PI/40, PI-PI/40, PI/80], ]
        start = VGroup(*[FunctionGraph(func(0), x_range=r)
                       for func, r in zip(taylor_list, ranges)])
        Texs = VGroup(
            Tex("\sin(x)=\sum_{n=0}^\infty\\frac{(-1)^n}{(2n+1)!}x^{2n+1}").to_corner(DL),
            Tex("\cos(x)=\sum_{n=0}^\infty\\frac{(-1)^n}{(2n)!}x^{2n}").to_corner(DL),
            Tex("\\tan(x)=\sum_{n=1}^\infty\\frac{B_{2n}(-4)^n\left(1-4^n\\right)}{(2n)!}x^{2n-1}","\quad x\in\left(-\\frac{\pi}{2},\\frac{\pi}{2}\\right)").to_corner(DL),
            Tex("\\cot(x)=\\sum_{n=0}^\\infty\\frac{(-4)^nB_{2n}}{(2n)!}x^{2n-1}","\quad x\in\left(0,\pi\\right)").to_corner(DL),
            Tex("\\sec(x)=\\sum_{n=0}^\\infty\\frac{E_{n}x^{2n}}{(2n)!}","\quad x\in\left(-\\frac{\pi}{2},\\frac{\pi}{2}\\right)").to_corner(DL),
            Tex("\\csc(x)=\\sum_{n=0}^\\infty\\frac{(-1)^{n}2\\left(1 -2^{2n-1}\\right)B_{2n}}{(2n)!}x^{2n-1}","\quad x\in\left(0,\pi\\right)").to_corner(DL)
        )
        for i in Texs:
            i.scale(.7).to_corner(DL)
        grid = NumberPlane()
        self.play(Write(grid))
        self.wait()
        for i in range(6):
            self.play(ShowCreation(target[i]))
            self.play(Write(Texs[i][0]))
            if i>1:
                self.wait()
                self.play(Write(Texs[i][1]))
            self.wait()
            self.add(start[i],Texs[i])
            self.play(ShowCreation(start[i]))
            self.wait()
            for j in range(1,11):
                self.wait()
                self.add(start[i],Texs[i])
                self.play(start[i].animate.become(FunctionGraph(taylor_list[i](j),x_range=ranges[i])))
            self.wait()
            self.play(FadeOut(target[i]),FadeOut(start[i]),FadeOut(Texs[i]))
            self.wait()
        self.play(FadeOut(grid))
        self.wait(2)
        texttext = TexText("静静等待$\mathrm{BGM}$放完……")
        self.add(texttext)
        self.wait(11.5)
        print(self.time)



class together(Scene):
    def construct(self):
        Scene1_intro.construct(self)
        Scene2_Guide_to_3B1B.construct(self)
        Scene3_Sine.construct(self)
        Scene4_Cosine.construct(self)
        Scene5_Tangent.construct(self)
        Scene6_Cotangent.construct(self)
        Scene7_Secent.construct(self)
        Scene8_Cosecent.construct(self)
        taylor.construct(self)


class thumbnail(Scene):
    def construct(self):
        tar = FunctionGraph(np.cos,x_range=[-2.5*PI,2.5*PI,PI/20],color=BLUE)
        tar2 = FunctionGraph(np.tan,x_range=[-PI/2+PI/40,PI/2-PI/40,PI/80],color=TEAL)
        appr = FunctionGraph(taylor_for_cos(2),x_range=[-2.5*PI,2.5*PI,PI/20],color=MAROON)
        appr2 = FunctionGraph(taylor_for_tan(3),x_range=[-PI,PI,PI/80],color=YELLOW)
        up = TexText("三角函数们的")
        text = TexText("泰勒级数").scale(3).to_edge(DOWN)
        up.next_to(text,UP)
        self.add(tar,tar2,appr,appr2,text,up)